/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.CuaHangDAO;
import dao.QuiDinhDAO;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.CuaHang;
import pojo.QuyDinh;

/**
 *
 * @author Phi
 */
public class QuanLyCuaHang extends javax.swing.JInternalFrame {

    /**
     * Creates new form QuanLyCuaHang
     */
    public QuanLyCuaHang() {
        initComponents();
    }
    public void XemDanhSachCuaHang() throws Exception{
        DefaultTableModel dtm=new DefaultTableModel();
        dtm.addColumn("Ma Cua Hang");
        dtm.addColumn("Ten Cua Hang");
        dtm.addColumn("Dia Chi"); 
        dtm.addColumn("Dien Thoai"); 
        List<CuaHang> ds=CuaHangDAO.layDanhSachCuaHang();
        dtm.setRowCount(ds.size());
        for(int i=0;i<ds.size();i++){
            CuaHang ch=ds.get(i);
            dtm.setValueAt(ch.getMaCuaHang(), i, 0);
            dtm.setValueAt(ch.getTenCuaHang(), i,1); 
            dtm.setValueAt(ch.getDiaChi(), i,2);
            dtm.setValueAt(ch.getDienThoai(), i,3);
        }
        this.jtbCuaHang.setModel(dtm);
    }
     public void them() throws Exception{
        String tenCuaHang=this.jtfTenCuaHang.getText();
       // String maCuaHang=this.jtfMaCuaHang.getText();
        //int ma=Integer.parseInt(maCuaHang);
        String diaChi=this.jtfDiaChi.getText();
        String dienThoai=this.jtfDienThoai.getText();
        CuaHang ch=new CuaHang();
        ch.setTenCuaHang(tenCuaHang);
        //ch.setMaCuaHang(ma);
        ch.setDienThoai(dienThoai);
        ch.setDiaChi(diaChi);
        boolean kq=CuaHangDAO.themThongTinCuaHang(ch);
        if(kq==false){
            JOptionPane.showMessageDialog(this,"Thêm thất bại ");         
        }
        else 
            JOptionPane.showMessageDialog(this,"Thêm thành công");
            this.XemDanhSachCuaHang();
    }
     
      public void xoa() throws Exception{
        int dong=this.jtbCuaHang.getSelectedRow();
        int maCuaHang=(Integer)this.jtbCuaHang.getValueAt(dong, 0);
        boolean kq=CuaHangDAO.xoaCuaHang(maCuaHang);
        if(kq==false){
            JOptionPane.showMessageDialog(this,"Xoá thất bại ");         
        }
        else 
            JOptionPane.showMessageDialog(this,"Xoá thành công");
            this.XemDanhSachCuaHang();
    }
      
      public void them_capnhat() throws Exception{
        String tenCuaHang=this.jtfTenCuaHang.getText();
       // String maCuaHang=this.jtfMaCuaHang.getText();
        //int ma=Integer.parseInt(maCuaHang);
        String diaChi=this.jtfDiaChi.getText();
        String dienThoai=this.jtfDienThoai.getText();
        CuaHang ch=new CuaHang();
        ch.setTenCuaHang(tenCuaHang);
        //ch.setMaCuaHang(ma);
        ch.setDienThoai(dienThoai);
        ch.setDiaChi(diaChi);
        boolean kq=CuaHangDAO.themThongTinCuaHang(ch);
        if(kq==false){
            JOptionPane.showMessageDialog(this,"Cập nhật thất bại ");         
        }
        else 
            JOptionPane.showMessageDialog(this,"Cập nhật thành công");
            this.XemDanhSachCuaHang();
    }
      
      public void xoa_capnhat() throws Exception{
        int dong=this.jtbCuaHang.getSelectedRow();
        int maCuaHang=(Integer)this.jtbCuaHang.getValueAt(dong, 0);
        boolean kq=CuaHangDAO.xoaCuaHang(maCuaHang);
    }
      public void capNhat() throws Exception{
          this.xoa_capnhat();
          this.them_capnhat();
      }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtThem = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtTimKiem = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jtfDienThoai = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtfDiaChi = new javax.swing.JTextField();
        jtfTenCuaHang = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbCuaHang = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Quản Lý Cửa Hàng");

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtTimKiem.setText("Cập Nhật");
        jbtTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtTimKiemActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin cửa hàng"));

        jLabel4.setText("Điện Thoại");

        jtfDienThoai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDienThoaiActionPerformed(evt);
            }
        });

        jLabel3.setText("Địa Chỉ");

        jLabel1.setText("Tên Cửa Hàng");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfTenCuaHang)
                    .addComponent(jtfDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addComponent(jtfDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfTenCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jtfDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách cửa hàng"));

        jtbCuaHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbCuaHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbCuaHangMouseClicked(evt);
            }
        });
        jtbCuaHang.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jtbCuaHangAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(jtbCuaHang);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtTimKiem)
                        .addGap(89, 89, 89))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtThem)
                    .addComponent(jbtXoa)
                    .addComponent(jbtTimKiem))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtbCuaHangAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jtbCuaHangAncestorAdded
        try {
            this.XemDanhSachCuaHang();
        } catch (Exception ex) {
            Logger.getLogger(QuanLyCuaHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jtbCuaHangAncestorAdded

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        try {
            this.them();
        } catch (Exception ex) {
            Logger.getLogger(QuanLyCuaHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        try {
            this.xoa();
        } catch (Exception ex) {
            Logger.getLogger(QuanLyCuaHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jtfDienThoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDienThoaiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfDienThoaiActionPerformed

    private void jbtTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtTimKiemActionPerformed
        try {
            this.capNhat();
        } catch (Exception ex) {
            Logger.getLogger(QuanLyCuaHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtTimKiemActionPerformed

    private void jtbCuaHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbCuaHangMouseClicked
        int dong =this.jtbCuaHang.getSelectedRow();
        String maCH=this.jtbCuaHang.getValueAt(dong, 1).toString();
        String dienThoai=this.jtbCuaHang.getValueAt(dong, 2).toString();
        String diaChi=this.jtbCuaHang.getValueAt(dong, 3).toString();
        this.jtfTenCuaHang.setText(maCH);
        this.jtfDienThoai.setText(dienThoai);
        this.jtfDiaChi.setText(diaChi);
    }//GEN-LAST:event_jtbCuaHangMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtTimKiem;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JTable jtbCuaHang;
    private javax.swing.JTextField jtfDiaChi;
    private javax.swing.JTextField jtfDienThoai;
    private javax.swing.JTextField jtfTenCuaHang;
    // End of variables declaration//GEN-END:variables
}
